home *** CD-ROM | disk | FTP | other *** search
/ Archive Magazine CD 1995 / Archive Magazine CD 1995.iso / discs / prog_disc / volume_5 / issue_09 / power / ReplaceDoc < prev    next >
Encoding:
Text File  |  1992-05-29  |  10.7 KB  |  257 lines

  1.  
  2. REPLACE: Search & Replace Pattern Programming Compiler 29th May '92
  3.  
  4. Replace 1.00: Copyright (C) 1991 Michael Houlder
  5.  
  6.  
  7. 0. NOTE
  8. =======
  9. Please read this file using the system font.
  10.  
  11.  
  12. 1. Purpose
  13. ==========
  14. REPLACE acts as a compiler for a search and replace pattern language based on UNIX regular
  15. expressions and very similar to that used by Twin and !SrcEdit.
  16.  
  17. TWIN is the original screen text editor which runs under the command line interpreter.
  18. !SrcEdit is the new multi-tasking text editor supplied as part of Acorn's new Desktop
  19. Development Environment (DDE).
  20.  
  21. The 'wildcarded expression' facility allows structures within complex data files to be
  22. described, found and modified in a simple data-oriented manner which requires the
  23. minimum of concern for normal programming considerations.
  24.  
  25. The ordered execution of many search & replace instructions on the same data can have the
  26. effect of sophisticated procedural programming.
  27.  
  28. The compiler takes as its input a language source ASCII text file and, possibly multiple,
  29. data files.
  30.  
  31. The use of a permanent disk text file to hold the language source allows accumulation of
  32. many search & replace instructions for ordered execution on the same data. Hence REPLACE
  33. provides a new style of programming: pattern programming.
  34.  
  35. With a permanent disk file, development and accumulation of a program over a period of time is
  36. possible.
  37.  
  38. In thes respect, REPLACE differs from both TWIN and !SrcEdit. These take their search and
  39. replace instructions from the keyboard and no permanent record of the instruction is available
  40. for future use.
  41.  
  42. The opportunity in writing REPLACE was taken to formalise fully the syntax, and in particular,
  43. the semantics of the search & replace language defined by !SrcEdit.
  44.  
  45. However, !SrcEdit and REPLACE can be used together to considerable advantage. !SrcEdit allows
  46. the development and testing of fragments of a REPLACE pattern program on an interactive and
  47. incremental basis.
  48.  
  49.  
  50. 2. References
  51. =============
  52. (a) The DDE format !help files contained in the two applications associated with this
  53. documentation: !Replace and !Styles.
  54.  
  55. (b) The 3 parts of my article "Power Search: A Quiet AI Revolution" contained in Archive
  56. magazine, Vols 5.7, 5.8, 5.9 for April, May and June respectively. These are essential to
  57. understand the semantics of the pattern programming language.
  58.  
  59. (c) The Acorn User Guide for TWIN, pages 25 to 32.
  60.  
  61. (d) The Acorn DDE User Guide for !SrcEdit, pages 99 to 103.
  62.  
  63. (e) SUN UNIX documentation for LEX, page 123 onwards.
  64.  
  65. (f) Tony Mason & Doug Brown, "Lex & Yacc", O'Reilly & Associates 1990, page 116 onwards.
  66. This is a reasonably good, but quite expensive, introduction to the notorious UNIX
  67. language development tools, LEX (LEXical analyser) & YACC (Yet Another Compiler Compiler).
  68.  
  69. (g) Kleene S.C., "Representation of events in nerve nets and finite automata", Rand Memorandum,
  70. Dec 1951. This is the absolutely fundamental academic paper in which Prof. Kleene introduced
  71. the notion of 'regular expressions'.
  72.  
  73.  
  74. 3. Restricted Function
  75. ======================
  76. REPLACE 1.00 is, hopefully, not the last word in pattern programming. I have version 2 under
  77. test at the present time which allows recursion equivalent iteration, conditionals,
  78. functional abstraction, libraries & more. In the development of the language, the aim is to
  79. minimise procedural algorithmic programming.
  80.  
  81.  
  82. 4. Definitions
  83. ==============
  84. The basic idea is pattern-matching with replacement. Search and Data patterns match or
  85. correspond. The way they correspond defines a replacement string which is substituted for
  86. the data pattern.
  87.  
  88. The search pattern is given by a sequence of terms in the search pattern language. The terms
  89. and the way they come together are described by the grammar given below.
  90.  
  91. The search sequence (of terms) corresponds to that entered into the "Find" writeable
  92. icon in the !SrcEdit "Find Text" dialogue.
  93.  
  94. The replace sequence is a sequence of terms in the replacement pattern language. This is
  95. described also by a grammar below. The sequence defines a pattern since the replacement
  96. string does not have to be constant but can depend on the match actually found between
  97. the search pattern and the data.
  98.  
  99. The replace sequence (of terms) corresponds to that entered into the "Replace with"
  100. writeable icon in the !SrcEdit "Find Text" dialogue.
  101.  
  102. There one syntax difference between REPLACE and that of !SrcEdit. Sequences in REPLACE
  103. must be expressible using ASCII text. !SrcEdit uses the symbol ☓ to define a HEX character
  104. code. This is not an ASCII symbol. So, REPLACE uses '!' as an equivalent: e.g. "!09" for
  105. "☓09" which stands for a tab character.
  106.  
  107. Both search and replace sequences in REPLACE must be surrounded by quotes: e.g. "!09".
  108. A replace sequence may be null: e.g. "".
  109.  
  110. A search sequence and a replace sequence combine to form a TRANSFORM. A transform is written
  111. using connectives '=>' and ';':
  112.  
  113.                 e.g. "!09" => "";
  114.  
  115. This transform replaces the tab character with the empty string, i.e. deletes the tab.
  116.  
  117.  
  118. A pattern program consists of a list of transforms: e.g.
  119.  
  120.         "!09" => "";            /* deletes tabs */
  121.         "$" => "";              /* deletes newlines (linefeeds) */
  122.         " " => "";              /* deletes spaces */
  123.  
  124. Comments are defined by enclosing the comment within /* . . . */. This follows the C
  125. programming language convention. Comments may occur anywhere except within either a
  126. search or a replace sequence.
  127.  
  128. Should a search sequence need to refer to a comment enclosing symbol, it will need to
  129. use the 'normal' operator to take the symbol '*' as a natural: e.g.
  130.  
  131.         "/\**.\*/" => "";       /* deletes comments */
  132.  
  133.  
  134. 5. Operation
  135. ============
  136. A multi-tasking interface in accordance with the DDE standard is provided and there are
  137. two significant modes of operation: compilation, compilation with execution.
  138.  
  139. Compilation only occurs when a program but no data input is provided. For REPLACE 1.00,
  140. this gives an opportunity for the compiler to say what it thinks it has been given
  141. with the program source. It does this via a listing which translates back the compiled
  142. pseudo code into which the source was translated.
  143.  
  144. Compilation with execution occurs when both program source and data files are provided.
  145.  
  146. Each transform in turn is taken from the list and is used from the start of each data
  147. file to the end to find matches with automatic replacement. This the equivalent to a
  148. global replacement or the selection of the "End of file Replace" icon in the !SrcEdit
  149. "Text Found" dialogue.
  150.  
  151. For documentation on the way the matches are found, or how replacements are made, please
  152. read the Archive article referred to above: "Power Search: The Quiet AI Revolution".
  153.  
  154.  
  155. 6. Search Sequence Grammar
  156. ==========================
  157.    *  a search sequence is a number, greater than 0, of search components 
  158.       set together in a sequence or list. At least one component of which is 
  159.       not a '0 or more' repetition.
  160.  
  161.    *  a search component is one of: a specific character, a specific string, 
  162.       a set of characters, or a repetition.
  163.  
  164.    *  a specific character is any character that is not a search pattern 
  165.       control symbol unless it has been normalised by the normal 
  166.       operator.
  167.  
  168.    *  a specific string is any string composed of specific characters put 
  169.       together.
  170.  
  171.    *  a set of characters is either a pre-defined set or a user-defined set
  172.  
  173.    *  a repetition is a repetition control symbol followed by either a 
  174.       specific character or a set of characters.
  175.  
  176.    *  a user-defined set is either a bracketed set or a negated set.
  177.  
  178.    *  a search pattern control symbol is one of: a pre-defined set symbol, a 
  179.       set construction symbol, a repetition control symbol, or a character 
  180.       operator.
  181.  
  182.    *  a pre-defined set is one of the four symbols: '.', '$', '@', '#'. 
  183.       Respectively, these are named for interpretation purposes as: 'any', 
  184.       'newline', 'alphanum', 'digit'.
  185.  
  186.    *  a set construction symbol is one of the four symbols: '[', ']', '~', '-'. 
  187.       Respectively, these are named as: 'left set', 'right set', 'not', and 
  188.       'to'.
  189.  
  190.    *  a repetition control symbol is one of the three symbols: '*', '^', '%'. 
  191.       Respectively, these are named as: '0 or more', '1 or more', or 
  192.       'most'.
  193.  
  194.    *  a character operator is one of the three symbols: '\', '|', '!'. 
  195.       Respectively, these are named as: 'normal', 'control', or 'hex' 
  196.       operators. They are not part of the search sequence themselves. 
  197.       They change the status of the character or characters that follow 
  198.       them, either by normalising search pattern control characters or by 
  199.       re-coding hexadecimal or non-printable ASCII values. !SrcEdit uses 
  200.       a non-printable graphics symbol '☓' instead of '!'.
  201.  
  202.    *  a bracketed set is a combination of character lists and character 
  203.       ranges put together in any order between a 'left set' bracket and a 
  204.       'right set' bracket
  205.  
  206.    *  a character list is a list composed of characters that are not set 
  207.       construction symbols unless they are normalised or out of context. 
  208.       For instance, in "[abc[d]", the second '[' cannot be 'left set' and is, 
  209.       therefore, out of context. Again in "[-xyz]", '-' cannot be the range 
  210.       symbol 'to'.
  211.  
  212.    *  a character range is a list of three characters with the middle one 
  213.       being the 'to' symbol and the outer two being characters that are not 
  214.       set construction symbols unless they are normalised or out of 
  215.       context. The outer two are not required to be in any order; i.e. "[a-t]" 
  216.       is equivalent to "[t-a]".
  217.  
  218.    *  a negated set is the set construction symbol 'not' followed by either 
  219.       a specific character, a pre-defined set symbol, or a bracketed set. 
  220.       Such a component may not be null; "~." is excluded.
  221.  
  222.    *  For no good reason, !SrcEdit excludes the repetition "%.". As this 
  223.       component is important, standing for "the rest of the file", Replace 
  224.       supports it.
  225.  
  226.  
  227. 7. Replace Sequence Grammar
  228. ===========================
  229.    *  a replace sequence is a number, greater or equal to 0, of replace components set together in a 
  230.       sequence or list.
  231.  
  232.    *  a replace component is one of: a specific character, a specific string, a newline, a numbered field, 
  233.       or a found string.
  234.  
  235.    *  a specific character is any character excluding three: '$', '?', '&', unless they have been 
  236.       normalised by the normal operator.
  237.  
  238.    *  a specific string is any string composed of specific characters put together.    
  239.  
  240.    *  a newline is the replace control symbol '$'.
  241.  
  242.    *  a numbered field is the replace control symbol '?' followed by a single decimal digit, '0' to '9'.
  243.  
  244.    *  a found string is the replace control symbol '&'.
  245.  
  246.  
  247. 8. Feedback
  248. ===========
  249. Feedback and suggestions coming from evaluation and use of the compiler would be most welcome.
  250. Please contact:
  251.  
  252.         Mike Houlder,
  253.         6 Worrall Road,
  254.         Sheffield,
  255.         South Yorkshire S6 4BA
  256.  
  257.